package com.shepin.Util;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;

/**
 * Created by apple on 16/7/12.
 */
public class Util extends Application {

    private static Context context;
    private static Util util;
    public File MROPath;
    public static String SDPATH;
    public static String IMG_SAVE_PATH;
    public static String IMG_SHARE_PATH;
    public static String APK_LOG_PATH;
    public static String APK_INSTALL_PATH;
    public static String APP_DATA_ROOT_PATH;
    public static String IMG_SAVE_PATH_CAP_TEMP;
    public static String IMG_SAVE_PATH_CAP_CUT;
    public static String IMG_CACHE_XUTILS_SDCARD_PATH;
    public static String IMG_CACHE_XUTILS_DEFAULT_PATH;
    public static String FINAL_IMAGE_PATH;
    public static String FINAL_TEMP_PATH;

    public File ImgCachePath;
    public File ImgSavePath;
    public File ImgSharePath;
    public File ApkSavePath;
    public File LogSavePath;
    public File ImgCapTempPath;
    public File ImgCapCutPath;
    public File ImgCacheDefaultPath;

    @Override
    public void onCreate() {
        super.onCreate();
        util=this;
        init_path();
    }
    public String getAppPath(){
        return context.getFilesDir().getParent()+"/";
    }

    /**
     * 初始化缓存路径
     */
    public void init_path(){
        Log.e("Util", "checking folder");
        if(Sys.isSDcardExist()){
            SDPATH= Environment.getExternalStorageDirectory()+"/";
            IMG_SAVE_PATH = SDPATH + "MRO/images/save";
            IMG_SHARE_PATH = SDPATH + "MRO/images/share";
            APK_INSTALL_PATH = SDPATH + "MRO/app/";
            APK_LOG_PATH = SDPATH + "MRO/log/";
            IMG_SAVE_PATH_CAP_TEMP = SDPATH + "MRO/images/save/capture/mro_temp";
            IMG_SAVE_PATH_CAP_CUT = SDPATH + "MRO/images/save/capture/mro_cut";
            IMG_CACHE_XUTILS_SDCARD_PATH = SDPATH + "MRO/images/cache/";
            IMG_CACHE_XUTILS_DEFAULT_PATH = SDPATH + "Android/data/" + context.getPackageName() + "/cache/imgCache/";
            APP_DATA_ROOT_PATH = getAppPath() + "MRO/";
            Log.i(Var.info, "FINAL_DATA_ROOT_PATH = " + APP_DATA_ROOT_PATH);
            Log.i(Var.info, "IMG_CACHE_XUTILS_DEFAULT_PATH = " + IMG_CACHE_XUTILS_DEFAULT_PATH);

            FINAL_IMAGE_PATH = APP_DATA_ROOT_PATH + "images/";
            FINAL_TEMP_PATH = APP_DATA_ROOT_PATH + "temp/";

            MROPath = new File(APP_DATA_ROOT_PATH);
            if (!MROPath.exists()) {
                MROPath.mkdirs();
            }
            MROPath = new File(FINAL_IMAGE_PATH);
            if (!MROPath.exists()) {
                MROPath.mkdirs();
            }

            MROPath = new File(FINAL_TEMP_PATH);
            if (!MROPath.exists()) {
                MROPath.mkdirs();
            }

            // 拍照图片保存地址
            ImgCapTempPath = new File(IMG_SAVE_PATH_CAP_TEMP);
            if (!ImgCapTempPath.exists()) {
                ImgCapTempPath.mkdirs();
            }
            // 裁剪后图片保存地址
            ImgCapCutPath = new File(IMG_SAVE_PATH_CAP_CUT);
            if (!ImgCapCutPath.exists()) {
                ImgCapCutPath.mkdirs();
            }
            // 图片保存、缓存地址
            ImgSavePath = new File(IMG_SAVE_PATH);
            if (!ImgSavePath.exists()) {
                ImgSavePath.mkdirs();
            }
            // 分享图片的临时保存路径
            ImgSharePath = new File(IMG_SHARE_PATH);
            if (!ImgSharePath.exists()) {
                ImgSharePath.mkdirs();
            }
            // 检测更新时保存路径
            ApkSavePath = new File(APK_INSTALL_PATH);
            if (!ApkSavePath.exists()) {
                ApkSavePath.mkdirs();
            }
            // 异常保存路径
            LogSavePath = new File(APK_LOG_PATH);
            if (!LogSavePath.exists()) {
                LogSavePath.mkdirs();
            }
            ImgCachePath = new File(IMG_CACHE_XUTILS_SDCARD_PATH);
            if (!ImgCachePath.exists()) {
                ImgCachePath.mkdirs();
            }
            ImgCacheDefaultPath = new File(IMG_CACHE_XUTILS_DEFAULT_PATH);
            if (!ImgCacheDefaultPath.exists()) {
                ImgCacheDefaultPath.mkdirs();
            }

        } else {
            Log.e("Util", " SD Card does not exist ...");
            APP.Tip("请插入外置存储卡");
        }
    }


    public static Util getInstance(Context context){
        Util.context=context;
        if(util==null){
            synchronized (Util.class){
                if(util==null)
                    util=new Util();
            }
        }
        return util;
    }

}
